import numpy as np
import matplotlib.pyplot as plt
from math import ceil, floor, sqrt

'''
flow completion time(FCT)
Average FCTs
99th %-tile FCTs

(0,100KB]: Avg 
(0,100KB]: 99th Percentile 
(100KB,10MB]: Avg
(10MB,++): Avg
'''

plt.figure(figsize=(6,4))
overall_fct = []
with open( "Detour_VL2_16ToRs_4Hosts_4Links_0.6Load_1Time.txt", 'r') as f:
    for line in f.readlines():
    	split = line.split()
        overall_fct.append(float(split[3])*1000)   
overall_fct.sort()
print len(overall_fct)

# ovreall average FCT
overall_sum_fct = 0
for x in overall_fct:
	overall_sum_fct += x
overall_avg_fct = overall_sum_fct/len(overall_fct)
print overall_sum_fct, overall_avg_fct

# 99th %-tile average FCT
tail99_fct = overall_fct[-len(overall_fct)/100+1:]
tail99_sum_fct = 0
for x in tail99_fct:
	tail99_sum_fct += x
tail99_avg_fct = tail99_sum_fct/len(tail99_fct)
print tail99_sum_fct,tail99_avg_fct


cdfx = overall_fct
cdfy = np.linspace(1 / len(overall_fct), 1.0, len(overall_fct))

fs = 14
plt.semilogx(cdfx, cdfy,'r', linewidth=1, label='Ideal')
plt.xlabel('FCT(ms)', fontsize=fs)
plt.ylabel('CDF', fontsize=fs)
plt.legend(loc=4, fontsize=fs)
plt.subplots_adjust(bottom=0.15, top=0.9, left=0.12, right=0.95)
plt.grid()
plt.show()